import { defineStore } from 'pinia'
import { userLoginService } from '@/api/user'
import { ref } from 'vue'
import { useCartStore } from './cart'

export const useUserStore = defineStore(
  'user',
  () => {
    const cartStore = useCartStore()

    // 用户数据
    const userInfo = ref({})
    // 获取用户数据
    const getUserInfo = async ({ account, password }) => {
      const res = await userLoginService({ account, password })
      // console.log(res)
      // console.log(res.data.result)
      // 存储用户信息
      userInfo.value = res.data.result
    }

    // 清空用户数据
    const clearUserInfo = () => {
      userInfo.value.userInfo = ''
      userInfo.value = {}
    }

    // 暴露出去
    return {
      userInfo,
      getUserInfo,
      clearUserInfo,
    }
  },
  {
    persist: true,
  },
)
